// 获取路由列表
export function getMenuList(menuList) {
  let list = []
  menuList.forEach(v => {
    if (v.children && v.children.length > 0) {
      getMenu(v, list)
    } else {
      list.push(v)
    }
  })
  return list
}

export function getMenu(menu, list) {
  if (menu.children && menu.children.length > 0) {
    menu.children.forEach(v => {
      list.push(v)
      getMenu(v, list)
    })
  }
  return menu
}

// 获取顶级菜单索引
export function getCurrentMenuIndex(code) {
  const menuList = this.$store.getters.menuList
  return menuList.findIndex(v => {
    if (v.component === code) {
      return true
    } else if (v.children && v.children.length > 0) {
      return !!getMenuByCode(v.children, code)
    }
  })
}

// 获取指定菜单对象
export function getMenuByCode(menuList, code) {
  let menu
  for (let i = 0; i < menuList.length; i++) {
    const v = menuList[i]
    if (v.component === code) {
      menu = v
    } else if (v.children && v.children.length > 0) {
      menu = getMenuByCode(v.children, code)
    }
    if (menu) {
      break
    }
  }
  return menu
}

// 通过当前路由名称获取导航列表
export function getNavigatorMenuList(code) {
  let menuList = this.$store.getters.menuList,
    list = []
  const menu = getMenuByCode(menuList, code)
  if (menu) {
    const codeList = menu.url.substring(1).split('/')
    codeList.forEach(v => {
      const _menu = menuList.find(vv => vv.component === v)
      list.push(_menu)
      menuList = _menu.children
    })
  }
  return list
}

// 获取顶级菜单下首个菜单
export function getFirstMenu(index) {
  const menuList = this.$store.getters.menuList
  if (menuList.length > 0) {
    const menu = menuList[index]
    if (menu.children && menu.children.length > 0) {
      return menu.children[0].component
    } else {
      return menu.component
    }
  }
  return ''
}
